package com.briup.test;

/**
 * @author 甜圆
 * @version v1.0.1
 * @date 2025/9/25 11:26
 * @package com.briup.test
 * @copyright 甜圆
 */
public class Unicycle {
    // 独轮车状态
    private String state = "闲置";

    // synchronized修饰方法
    public synchronized void ride(String name) {
        state = name + "正在骑";
        System.out.println(state);
        try {
            // 模拟骑车时间
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        state = "闲置";
        System.out.println(name + "骑完了，现在" + state);
    }

    public static void main(String[] args) {
        Unicycle unicycle = new Unicycle();

        // 三个同学想骑独轮车
        Thread student1 = new Thread(() -> unicycle.ride("小明"));
        Thread student2 = new Thread(() -> unicycle.ride("小红"));
        Thread student3 = new Thread(() -> unicycle.ride("小刚"));

        student1.start();
        student2.start();
        student3.start();
    }
}
